﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DCEdu;
using System.Data.SqlClient;
using Utils;
using Utils.DatabaseUtils;

public partial class Search_Customer : System.Web.UI.Page
{
    DataSet dataSet;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            dataSet = new Customers().GetAll();
        }
        catch (Exception ex)
        {
            MyMessage.ShowMessage("Lỗi kết nối với cơ sở dữ liệu");
            Response.Write(ex.ToString());
        }

        if (IsPostBack) { return; }            
            
        cbxAddress.DataSource = dataSet;
        cbxEmail.DataSource = dataSet;
        cbxID.DataSource = dataSet;
        cbxName.DataSource = dataSet;
        cbxPerson.DataSource = dataSet;
        cbxTelePhone.DataSource = dataSet;

        cbxAddress.DataBind();
        cbxEmail.DataBind();
        cbxID.DataBind();
        cbxName.DataBind();
        cbxPerson.DataBind();
        cbxTelePhone.DataBind();

        GridView1.DataSource = dataSet;
        GridView1.DataBind();       
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection sqlCon = null;
        dataSet = null;
        try
        {
            sqlCon = new SqlConnection(Common.Conn);
            SqlCommand cmd = new SqlCommand("Customers_Search", sqlCon);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@ID", Int32.Parse(cbxID.SelectedText.Trim())));
            cmd.Parameters.Add(new SqlParameter("@Name", cbxName.SelectedText.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Telephone", cbxTelePhone.SelectedText.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Address", cbxAddress.SelectedText.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Email", cbxEmail.SelectedText.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Person", cbxPerson.SelectedText.Trim()));
            
            sqlCon.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);            
            dataSet = new DataSet();
            adapter.Fill(dataSet);

            GridView1.DataSource = dataSet;
            GridView1.DataBind();

        }
        catch (Exception ex)
        {
            MyMessage.ShowMessage("Lỗi kết nối với cơ sở dữ liệu");
            Response.Write(ex.ToString());
            
        }
        finally
        {
            if (sqlCon != null) sqlCon.Close();            
        }
 
 

    }
}